<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="小区名称" prop="quarterName">
        <el-input v-model="queryParams.quarterName" placeholder="请输入小区名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="fullName">
        <el-input v-model="queryParams.fullName" placeholder="请输入姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输手机号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="看房时间" prop="payTime">
        <el-date-picker clearable size="small" v-model="dateRange" type="daterange" value-format="yyyy-MM-dd"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="小区" align="center" prop="quarterName" min-width="120" fixed="left">
        <template slot-scope="scope">
          <div v-if="scope.row.goods">
            <span>{{ scope.row.goods.quarterName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="房号" align="center" prop="roomLocation" width="140" fixed="left">
        <template slot-scope="scope">
          <div v-if="scope.row.goods">
            <span>{{ scope.row.goods.roomLocation }}</span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="订单号" align="center" prop="orderNo" min-width="120" /> -->
      <el-table-column label="姓名" align="center" prop="fullName" min-width="100" />
      <el-table-column label="手机号" align="center" prop="phone" min-width="120" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-for="(item, index) in statusOptions" :key="index">
            <el-tag v-if="item.value == scope.row.status" :type="item.type">{{ item.label }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="临时密码" align="center" prop="temporaryPwd" min-width="120" show-overflow-tooltip />
      <el-table-column label="审核人" align="center" prop="auditUserName" min-width="120" />
      <el-table-column label="看房时间" align="center" prop="bookingTime" min-width="150" />
      <el-table-column label="提交时间" align="center" prop="createTime" min-width="150" />
      <el-table-column label="驳回理由" align="center" prop="rejectReason" min-width="120" show-overflow-tooltip />
      <el-table-column label="操作" align="center" min-width="120" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handlePwd(scope.row)"
            v-show="scope.row.status == '1'" v-hasPermi="['order:info:pwd']">发送密码 </el-button>
          <span v-if="scope.row.status == '0'">
            <el-button size="mini" type="text" icon="el-icon-check" @click="handlePass(scope.row)"
              v-hasPermi="['order:info:pass']">通过</el-button>
            <el-button size="mini" type="text" icon="el-icon-close" @click="handleReject(scope.row)"
              v-hasPermi="['order:info:reject']">驳回 </el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改支付订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="驳回原因" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 发送密码 -->
    <el-dialog :title="pwd.title" :visible.sync="pwd.open" width="500px" append-to-body>
      <el-form ref="pwdForm" :model="pwd.form" :rules="pwd.rules" label-width="80px">
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="pwd.form.pwd" placeholder="请输入密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPwd">确 定</el-button>
        <el-button @click="cancelPwd">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, exportInfo, audit, sendPwd } from "@/api/order/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 支付订单信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        orderType: 'yykf_order',
        merchantOrderNo: null,
        status: null,
        wxClientId: null,
        payStatus: null,
        payTime: null,
        payType: null,
        wxUserId: null,
        totalAmount: null,
        payAmount: null,
        quarterName: null,
        fullName: null,
        phone: null,
        bookingTimeStart: null,
        bookingTimeEnd: null,
        // params: {
        //   bookingTimeStart: null,
        //   bookingTimeEnd: null
        // },
      },
      statusOptions: [
        { value: '0', label: '待审核', type: 'warning' },
        { value: '1', label: '预定成功', type: 'success' },
        { value: '2', label: '取消', type: 'info' },
        { value: '3', label: '驳回', type: 'danger' },
        { value: '5', label: '已完成', type: 'success' },
      ],
      // 表单参数
      form: {},
      // 日期范围
      dateRange: [],
      pwd: {
        title: "",
        open: false,
        form: {
          id: null,
          pwd: ""
        },
        rules: {
          pwd: [
            { required: true, message: '请填写密码', trigger: 'blur' },
          ]
        }
      },
      // 表单校验
      rules: {
        remark: [
          { required: true, message: '请填写驳回原因', trigger: 'blur' },
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询支付订单信息列表 */
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.bookingTimeStart = this.dateRange[0];
        this.queryParams.bookingTimeEnd = this.dateRange[1];
      }
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: v,
        orderType: 'yykf_order',
        merchantOrderNo: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        wxClientId: null,
        payStatus: null,
        payTime: null,
        payType: null,
        wxUserId: null,
        totalAmount: null,
        payAmount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = []
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付订单信息";
    },
    /** 修改按钮操作 */
    handleRefund(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "退看房押金";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.opt = "2"
          audit(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有支付订单信息数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportInfo(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () { });
    },
    /**
   * 审核
   */
    handleReject(row) {
      this.form = {}
      this.open = true;
      this.title = "驳回";
      this.form = row
    },
    /**
     * 通过
     */
    handlePass(row) {
      let data = {
        id: row.id,
        opt: "1"
      }
      this.$confirm('是否确认通过"' + row.fullName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return audit(data);
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () { });
    },
    /**
     * 打开弹框
     */
    handlePwd(row) {
      this.pwd.title = "修改密码"
      this.pwd.open = true
      this.pwd.form.id = row.id
    },
    /**
     * 取消
     */
    cancelPwd() {
      this.pwd.open = false
      this.pwd.form = {}
    },
    submitPwd() {
      this.$refs["pwdForm"].validate(valid => {
        if (valid) {
          sendPwd(this.pwd.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("发送成功");
              this.cancelPwd()
              this.getList();
            }
          });
        }
      });
    }
  }
};
</script>